//
//  HealthIndexViewController.m
//  SmartWatch
//
//  Created by zwj on 2022/11/25.
//

@interface HealthIndexView : UIView
- (instancetype)initWithFrame:(CGRect)frame index:(NSInteger)index;
@end

@implementation HealthIndexView


- (instancetype)initWithFrame:(CGRect)frame index:(NSInteger)index
 {
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = 45;
        switch (index) {
            case 0:
                [self backColor:0x16BBC6];
                break;
            case 1:
                [self backColor:0x3066F1];
                break;
            case 2:
                [self backColor:0x663AE3];
                break;
            case 3:
                [self backColor:0xFF5454];
                break;
            case 4:
                [self backColor:0xF69707];
                break;
            case 5:
                [self backColor:0x19BE47];
                break;
            case 6:
                [self backColor:0xFF5C16];
                break;
            default:
                break;
        }
        NSArray *array = @[
            @{@"img":@"monitor_shenjing",@"name":@"自律神经指数",@"value":@"59.0",@"status":@"不平衡"},
            @{@"img":@"monitor_xianti",@"name":@"粒腺体指数",@"value":@"86.0",@"status":@"平衡"},
            @{@"img":@"monitor_apt",@"name":@"ATP指数",@"value":@"51.0",@"status":@"不平衡"},
            @{@"img":@"monitor_xuetang",@"name":@"血指数",@"value":@"96.0",@"status":@"平衡"},
            @{@"img":@"monitor_qizhishu",@"name":@"气指数",@"value":@"14.0",@"status":@"不平衡"},
            @{@"img":@"monitor_xibao",@"name":@"细胞纠结",@"value":@"55.0",@"status":@"平衡"},
            @{@"img":@"monitor_fenmi",@"name":@"内分泌指数",@"value":@"84.0",@"status":@"不平衡"},
        ];
        
        MyLinearLayout *root = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
        root.myHorzMargin = 0;
        root.gravity = MyGravity_Horz_Center;
        root.myHeight = kAutoHeight;
        [self addSubview:root];
        
        UIImageView *image = [[UIImageView alloc]initWithImage:UIImageMake(array[index][@"img"])];
        image.mySize = CGSizeMake(32, 32);
        [root addSubview:image];
        
        UILabel *name = [UILabel createLabelText:array[index][@"name"] font:UIFontMake(12) textColor:COLOR_4];
        name.mySize = kAutoSize;
        [root addSubview:name];
        
        UILabel *value = [UILabel createLabelText:array[index][@"value"] font:UIFontMake(15) textColor:COLOR_4];
        value.myTop = 7;
        value.mySize = kAutoSize;
        [root addSubview:value];
        
        UILabel *status = [UILabel createLabelText:array[index][@"status"] font:UIFontMake(10) textColor:COLOR_4];
        status.myTop = 7;
        status.mySize = CGSizeMake(42, 16);
        status.textAlignment = NSTextAlignmentCenter;
        if([status.text isEqualToString:@"平衡"]){
            status.backgroundColor = HexColor(@"#E0F8EC");
            status.textColor = HexColor(@"#189E5B");
        }else{
            status.backgroundColor = HexColor(@"#FFD4D4");
            status.textColor = HexColor(@"#FF4545");
        }
        ViewCorner(status, 8);
        [root addSubview:status];
        
    }
    return self;
}

- (void)backColor:(uint32_t)value {
    [self az_setGradientBackgroundWithColors:@[[UIColor colorWithRGB:value alpha:1.0],[UIColor colorWithRGB:value alpha:0.6]] locations:@[@0.0,@1.0] startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1)];
}


@end

#import "HealthIndexViewController.h"
#import "WJCircleProgressView.h"
#import "HistoryReportView.h"
#import "ImportantRiskView.h"
#import "HealthDetailViewController.h"
#import "AnalyReportViewController.h"
#import "OrganDetailViewController.h"
#define radius 51
@interface HealthIndexViewController ()
@property (nonatomic, strong) WJCircleProgressView *progressView;
@end

@implementation HealthIndexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHideNavigationBar = YES;
    self.view.backgroundColor = BACKGROUNDCOLOR;
}

- (void)initSubviews {
    [super initSubviews];
    
    UIImageView *backImage = [[UIImageView alloc]initWithImage:UIImageMake(@"monitor_health_back")];
    backImage.frame = CGRectMake(0, 0, kScreenWidth, 220);
    [self.view addSubview:backImage];
    
    MyLinearLayout *navView = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    navView.myHorzMargin = 0;
    navView.gravity = MyGravity_Vert_Center | MyGravity_Horz_Between;
    navView.myHeight = 44;
    navView.myTop = StatusBarHeight;
    navView.padding = UIEdgeInsetsMake(0, 15, 0, 15);
    [self.view addSubview:navView];
    
    {
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setImage:UIImageMake(@"icon_arrow_left") forState:UIControlStateNormal];
        backBtn.mySize = CGSizeMake( 40, 40);
        backBtn.imageEdgeInsets = UIEdgeInsetsMake(10,10,10,10);
        [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [navView addSubview:backBtn];
        
        UILabel *content = [UILabel createLabelText:@"健康指数" font:UIFontBoldMake(17) textColor:COLOR1515];
        content.mySize = kAutoSize;
        [navView addSubview:content];
        
        UIButton *right = [UIButton createButtonText :@"历史报告" font:UIFontMake(14) textColor:COLOR1515];
        right.mySize = kAutoSize;
        [right addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
        [navView addSubview:right];
    }
    
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, NavigationContentTop, kScreenWidth, kScreenHeight - NavigationContentTop)];
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    
    MyLinearLayout *root = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    root.myHorzMargin = 15;
    root.myHeight = kAutoHeight;
    root.subviewVSpace = 15;
    [scrollView addSubview:root];
    
    MyLinearLayout *top = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    top.myHorzMargin = 0;
    top.myTop = 30;
    top.padding = UIEdgeInsetsMake(15, 15, 15, 15);
    top.layer.cornerRadius = 15;
    top.backgroundColor = COLOR_4;
    top.myHeight = kAutoHeight;
    [root addSubview:top];
    
    {
        UIImageView *avatarImage = [[UIImageView alloc]initWithImage:UIImageMake(@"avatar")];
        avatarImage.useFrame = YES;
        avatarImage.frame = CGRectMake(22, -20, 50, 50);
        [top addSubview:avatarImage];
        
        MyLinearLayout *nameLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        nameLay.myHorzMargin = 0;
        nameLay.gravity = MyGravity_Vert_Center | MyGravity_Horz_Between;
        nameLay.myHeight = kAutoHeight;
        nameLay.paddingLeft = 70;
        [top addSubview:nameLay];
        
        {
            UILabel *name = [UILabel createLabelText:@"王春芳" font:UIFontBoldMake(15) textColor:COLOR1515];
            name.mySize = kAutoSize;
            [nameLay addSubview:name];
            
            UILabel *time = [UILabel createLabelText:@"检测时间：2022-11-10" font:UIFontMake(13) textColor:HexColor(@"#888888")];
            time.mySize = kAutoSize;
            [nameLay addSubview:time];
        }
        
        MyLinearLayout *middleLabel = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        middleLabel.myHorzMargin = 0;
        middleLabel.gravity = MyGravity_Center;
        middleLabel.myHeight = 24;
        middleLabel.backgroundColor = HexColor(@"#F3FAFA");
        ViewCorner(middleLabel, 4);
        middleLabel.myTop = 15;
        [top addSubview:middleLabel];
        
        {
            UILabel *content = [UILabel createLabelText:@"最小检测时间间隔4小时，22点34分可再次检测" font:UIFontMake(12) textColor:HexColor(@"#538181")];
            content.mySize = kAutoSize;
            [middleLabel addSubview:content];
        }
        
        MyLinearLayout *bottomLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        bottomLay.myHorzMargin = 0;
        bottomLay.gravity = MyGravity_Vert_Center;
        bottomLay.myHeight = 100;
        bottomLay.subviewHSpace = 20;
        bottomLay.myTop = 15;
        [top addSubview:bottomLay];
        
        {
            UIView *leftView = [UIView new];
            leftView.myLeft = 15;
            leftView.mySize = CGSizeMake(110, 110);
            [bottomLay addSubview:leftView];
            
            {
                _progressView = [[WJCircleProgressView alloc] initWithFrame:CGRectMake(0, 0, 110, 110)];
                _progressView.circleRadius = 50;
                _progressView.circleBorderWidth = 8;
                _progressView.circleColor = HexColor(@"#E1F3F2");
                _progressView.progressColor = HexColor(@"#1EC2B4");
                _progressView.pointRadius = 5;
                _progressView.pointColor = HexColor(@"#1EC2B4");
                _progressView.pointBorderWidth = 2;
                _progressView.pointBorderColor = COLOR_4;
                [leftView addSubview:_progressView];
                _progressView.curProgress = 0.0;
                
                [self.progressView updateProgress:1.0 duration:5 completion:^{
                    NSLog(@"进度更新完成");
                }];
                
                MyLinearLayout *middleImage = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
                middleImage.myMargin = 15;
                middleImage.myTop = 23;
                middleImage.myHeight = 60;
                middleImage.backgroundImage = UIImageMake(@"monitor_quan");
                middleImage.gravity = MyGravity_Horz_Center;
                middleImage.subviewVSpace = 10;
                [leftView addSubview:middleImage];
                
                {
                    UILabel *name = [UILabel createLabelText:@"健康指数" font:UIFontMake(12) textColor:COLOR333];
                    name.myTop = 15;
                    name.mySize = kAutoSize;
                    [middleImage addSubview:name];
                    
                    UILabel *numberLabel = [UILabel new];
                    numberLabel.mySize = kAutoSize;
                    numberLabel.text = @"78.0";
                    NSString *leftNum = [numberLabel.text componentsSeparatedByString:@"."].firstObject;
                    NSString *rightNtm = [numberLabel.text componentsSeparatedByString:@"."].lastObject;
                    rightNtm = FormatString(@".%@",rightNtm);
                    numberLabel.attributedText = AttributedString(leftNum, UIFontBoldMake(30), THEME_COLOR, rightNtm, UIFontMake(14), THEME_COLOR, NSTextAlignmentLeft, 0);
                    [middleImage addSubview:numberLabel];
                }
            }
            
            
            MyLinearLayout *rightView = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
            rightView.weight = 1;
            rightView.myHeight = 100;
            rightView.subviewVSpace = 15;
            rightView.gravity = MyGravity_Center;
            [bottomLay addSubview:rightView];
            
            {
                MyLinearLayout *topLayout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
                topLayout.myHorzMargin = 0;
                topLayout.gravity = MyGravity_Center;
                topLayout.myHeight = 20;
                topLayout.subviewHSpace = 5;
                [rightView addSubview:topLayout];
                [topLayout setTarget:self action:@selector(topLayoutClick)];
                {
                    UIImageView *leftLogo = [[UIImageView alloc]initWithImage:UIImageMake(@"monitor_fengxian")];
                    leftLogo.mySize = CGSizeMake(18, 18);
                    [topLayout addSubview:leftLogo];
                    
                    UILabel *tishi = [UILabel createLabelText:@"重要风险预测" font:UIFontMake(13) textColor:HexColor(@"#888888")];
                    tishi.mySize = kAutoSize;
                    [topLayout addSubview:tishi];
                    
                    UIImageView *rightLogo = [[UIImageView alloc]initWithImage:UIImageMake(@"monitor_right")];
                    rightLogo.mySize = CGSizeMake(12, 12);
                    [topLayout addSubview:rightLogo];
                }
                
                UILabel *tishi = [UILabel createLabelText:@"无重大疾病风险" font:UIFontBoldMake(16) textColor:COLOR333];
                tishi.mySize = kAutoSize;
                [rightView addSubview:tishi];
            }
        }
    }
    
    MyLinearLayout *middle = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    middle.myHorzMargin = 0;
    middle.myHeight = kAutoHeight;
    middle.backgroundColor = COLOR_4;
    ViewCorner(middle, 15);
    middle.subviewVSpace = 15;
    middle.padding = UIEdgeInsetsMake(15, 15, 15, 15);
    [root addSubview:middle];
    
    {
        MyLinearLayout *nameLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        nameLay.myWidth = kScreenWidth - 60;
        nameLay.gravity = MyGravity_Center;
        nameLay.myHeight = 20;
        nameLay.subviewHSpace = 15;
        [middle addSubview:nameLay];
        
        
        for (int i = 0; i < 2; i++) {
            UIImageView *line = [[UIImageView alloc]initWithImage:UIImageMake(@"monitor_xian")];
            line.mySize = CGSizeMake(5, 12);
            [nameLay addSubview:line];
            
            if(i == 0){
                UILabel *name = [UILabel createLabelText:@"评估指数" font:UIFontMake(15) textColor:HexColor(@"#454545")];
                name.mySize = kAutoSize;
                [nameLay addSubview:name];
            }
        }
        
        UIView *backView = [[UIView alloc]init];
        backView.mySize = CGSizeMake(kScreenWidth - 60, kScreenWidth - 60);
        backView.backgroundColor = COLOR_5;
        [middle addSubview:backView];
        
        UIImageView *backImage = [[UIImageView alloc]initWithImage:UIImageMake(@"monitor_zhishu_back")];
        backImage.frame = CGRectMake(20, 20, kScreenWidth - 100, kScreenWidth - 100);
        [backView addSubview:backImage];
        
        CGFloat angle  = 0;// 第一次是30度，正指到指针竿
        
        for (int i = 0; i < 7; i ++) {
            UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake((kScreenWidth - 60)/2.0 - 45, 0, 90, (kScreenWidth - 60)/2.0)];
            itemView.backgroundColor = COLOR_5;
            [backView addSubview:itemView];
            // 转换角度
            CGPoint oldOrigin = itemView.frame.origin;
            itemView.layer.anchorPoint = CGPointMake(0.5, 1);
            CGPoint newOrigin = itemView.frame.origin;
            CGPoint transition;
            transition.x = newOrigin.x - oldOrigin.x;
            transition.y = newOrigin.y - oldOrigin.y;
            itemView.center = CGPointMake (itemView.center.x - transition.x, itemView.center.y - transition.y);

            CGAffineTransform transform = CGAffineTransformIdentity;
            transform = CGAffineTransformRotate(transform, M_PI*(angle + i*radius)/180);
            itemView.transform = transform;
            
            HealthIndexView *contentView = [[HealthIndexView alloc]initWithFrame:CGRectMake(0, 0, 90, 90) index:i];
            CGAffineTransform transformContent = CGAffineTransformMakeRotation(-M_PI*(angle + i*radius)/180);
            contentView.transform = transformContent;//旋转
            contentView.tag = 1000 + i;
            [itemView addSubview:contentView];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(healthItemClick:)];
            [contentView addGestureRecognizer:tap];
            
        }
    }
    
    
    MyLinearLayout *bottom = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    bottom.myHorzMargin = 0;
    bottom.gravity = MyGravity_Vert_Center | MyGravity_Horz_Around;
    bottom.myHeight = 56;
    bottom.backgroundColor = COLOR_4;
    ViewCorner(bottom, 15);
    [root addSubview:bottom];
    
    NSArray *array = @[
        @{@"name":@"健康报告",@"img":@"monitor_baogao"},
        @{@"name":@"器官能量",@"img":@"monitor_fei"},
    ];
    for (int i = 0; i < array.count; i++) {
        QMUIButton * itemBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [itemBtn setImage:UIImageMake(array[i][@"img"]) forState:UIControlStateNormal];
        itemBtn.mySize = kAutoSize;
        [itemBtn setTitle:array[i][@"name"] forState:UIControlStateNormal];
        [itemBtn setTitleColor:COLOR1515 forState:UIControlStateNormal];
        itemBtn.titleLabel.font = UIFontMake(15);
        itemBtn.imagePosition = QMUIButtonImagePositionLeft;
        itemBtn.spacingBetweenImageAndTitle = 10;
        itemBtn.tag = 100 + i;
        [itemBtn addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [bottom addSubview:itemBtn];
        
        if(i == 0){
            UIView *line = UIView.new;
            line.mySize = CGSizeMake(0.5, 26);
            line.backgroundColor = HexColor(@"#CECECE");
            [bottom addSubview:line];
        }
    }
    
}
#pragma mark - 历史报告
- (void)rightClick {
    
    HistoryReportView *resultView = [[HistoryReportView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 120, 220)];
    self.zh_popupController = [zhPopupController new];
    self.zh_popupController.layoutType = zhPopupLayoutTypeCenter;
    self.zh_popupController.allowPan = NO;
    self.zh_popupController.maskAlpha = 0.7;
    [self.zh_popupController presentContentView:resultView];
}
#pragma mark - 重要风险预测
- (void)topLayoutClick {
    ImportantRiskView *resultView = [[ImportantRiskView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 60, 380)];
    self.zh_popupController = [zhPopupController new];
    self.zh_popupController.layoutType = zhPopupLayoutTypeCenter;
    self.zh_popupController.allowPan = NO;
    self.zh_popupController.maskAlpha = 0.7;
    [self.zh_popupController presentContentView:resultView];
}

- (void)healthItemClick:(UITapGestureRecognizer *)ges {
    NSInteger tag = ges.view.tag - 1000;
    HealthDetailViewController *vc = HealthDetailViewController.new;
    [self showViewController:vc sender:self];
}
- (void)itemBtnClick:(UIButton *)sender {
    NSInteger tag = sender.tag - 100;
    if(tag == 0){
        AnalyReportViewController *vc = AnalyReportViewController.new;
        [self showViewController:vc sender:sender];
    }else {
        OrganDetailViewController *vc = OrganDetailViewController.new;
        [self showViewController:vc sender:self];
    }
    
    
}

- (void)back {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
@end
